package com.cloudbroker.bcs.csvc.lightinvesting.facade.service;

import com.cloudbroker.bcs.csvc.lightinvesting.api.service.LedService;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.InterestCertForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.RegForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.SendMsgForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.baseRelevanted.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.cashTransactions.AvailableMoneyForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.cashTransactions.BindCapitalAccountForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.cashTransactions.UnbundCapitalAccountForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.fansRelated.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.interestRelated.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.*;
import com.cloudbroker.bcs.csvc.lightinvesting.facade.service.aspect.UserCache;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.base.vo.BrokerVO;
import com.hundsun.jresplus.remoting.impl.annotation.ServiceParam;
import org.springframework.stereotype.Service;
import util.Base64;
import util.RSAEncrypt;
import util.UserTokenUtil;

import javax.annotation.Resource;

import java.util.Collections;

import static com.cloudbroker.bcs.csvc.lightinvesting.api.constants.LedT2FunctionIds.*;

/**
 * Created by fangyf18371 on 2016/7/15.
 */
@Service
public class LedServiceFacade implements LedService {
    @Resource
    private LedT2RemoteService ledT2RemoteService;
    private UserCache userCache;

    @Override
    public RegBackVo func_register_authuser(RegForm param) {
        return ledT2RemoteService.callT2(FUNC_COMUSER_EXREGISTER,param,RegBackVo.class);
    }

    @Override
    public ModAliasVo func_alias_mod(ModAliasForm param) {
        return ledT2RemoteService.callT2(FUNC_EXTCOMACCT_ALIAS_MOD,param,ModAliasVo.class);
    }

    @Override
    public CombineApplyVo func_combine_apply(CombineApplyForm param) {
        return ledT2RemoteService.callT2(FUNC_COMBINE_APPLY,param,CombineApplyVo.class);
    }

//    @Override
//    public StocksEntrustQryBackVo addStocksEntrust(StocksEntrustForm param) {
//        return ledT2RemoteService.callT2(FUNC_COMENTRUST_DETAIL_ORDER_ENTER,param,StocksEntrustQryBackVo.class);
//    }


    @Override
    public StocksEntrustVo func_stock_entrust_add(StocksEntrustForm param) {
        return ledT2RemoteService.callT2(FUNC_COMENTRUST_DETAIL_ORDER_ENTER,param,StocksEntrustVo.class);
    }

//    @Override
//    public CombEntrustQryBackVo addCombEntrust(CombEntrustForm param) {
//        return ledT2RemoteService.callT2(FUNC_COMENTRUST_ORDER_ENTER,param,CombEntrustQryBackVo.class);
//    }


    @Override
    public CombEntrustVo func_comb_entrust_add(CombEntrustForm param) {
        return ledT2RemoteService.callT2(FUNC_COMENTRUST_ORDER_ENTER,param,CombEntrustVo.class);
    }

    @Override
    public FocusCombineQryBackVo func_combine_collect_info_qry(FocusCombineForm param) {
        return ledT2RemoteService.callT2(FUNC_COMBINE_COLLECT_INFO_QRY,param,FocusCombineQryBackVo.class);
    }

    @Override
    public FocusInterestQryBackVo func_interest_focus(FocusInterestForm param) {
        return ledT2RemoteService.callT2(FUNC_COMUSER_COLLECT_QRY,param,FocusInterestQryBackVo.class);
    }

    @Override
    public SubscriptionCombineQryBackVo func_comb_subscription(SubscriptionCombineForm param) {
        return ledT2RemoteService.callT2(FUNC_COMBINE_FOCUS_INFO_QRY,param,SubscriptionCombineQryBackVo.class);
    }

//    @Override
//    public CombDetailQryBackVo combDetails(CombDetailForm param) {
//        return ledT2RemoteService.callT2(FUNC_COMBINE_BASIC_INFO_QRY,param,CombDetailQryBackVo.class);
//    }


    @Override
    public CombDetailQryBackVo func_comb_details(CombDetailForm param) {
        return ledT2RemoteService.callT2(FUNC_COMBINE_BASIC_INFO_QRY,param,CombDetailQryBackVo.class);
    }

    @Override
    public RecommendedCombQryBackVo func_comb_recommend(RecommendedCombForm param) {
        return ledT2RemoteService.callT2(FUNC_COMBINE_RECOMMEND_QRY,param,RecommendedCombQryBackVo.class);
    }

    @Override
    public CombRankQryBackVo func_comb_rank(CombRankForm param) {
        return ledT2RemoteService.callT2(FUNC_COMBINE_ORDER_QRY,param,CombRankQryBackVo.class);
    }

    @Override
    public CombineCollectVo func_combine_collect(CombineCollectForm param) {
        return ledT2RemoteService.callT2(FUNC_COMBINE_COLLECT,param,CombineCollectVo.class);
    }

    @Override
    public CombCollectCancelVo func_combine_collect_cancel(CombCollectCancelForm param) {
        return ledT2RemoteService.callT2(FUNC_COMBINE_COLLECT_CANCEL,param,CombCollectCancelVo.class);
    }

    //    @Override
//    public CombSubscribeQryBackVo combSubscribe(@ServiceParam("param") CombSubscribeForm param) {
//        return ledT2RemoteService.callT2(FUNC_COMBINE_FOCUS,param,CombSubscribeQryBackVo.class);
//    }


    @Override
    public CombSubscribeVo func_combine_focus(CombSubscribeForm param) {
        return ledT2RemoteService.callT2(FUNC_COMBINE_FOCUS,param,CombSubscribeVo.class);
    }

    @Override
    public CombSubscribeCancelVo func_combine_focus_cancel(CombSubscribeCancelForm param) {
        return ledT2RemoteService.callT2(FUNC_COMBINE_FOCUS_CANCEL,param,CombSubscribeCancelVo.class);
    }

    @Override
    public CombHistoryYieldQryBackVo func_comb_history_Yyield_query(CombHistoryYieldForm param) {
        return ledT2RemoteService.callT2(FUNC_COMBINE_HIS_INCOMERATIO_QRY,param,CombHistoryYieldQryBackVo.class);
    }

    @Override
    public CombApplyCloseVo func_comb_apply_close(CombApplyCloseForm param) {
        return ledT2RemoteService.callT2(FUNC_COMBINE_APPLY_Close,param,CombApplyCloseVo.class);
    }

//    @Override
//    public CombCostQryBackVo queryCombCost(CombCostForm param) {
//        return ledT2RemoteService.callT2(FUNC_COMBINE_FAREARG_QRY,param,CombCostQryBackVo.class);
//    }


    @Override
    public CombCostVo func_comb_cost_query(CombCostForm param) {
        return ledT2RemoteService.callT2(FUNC_COMBINE_FAREARG_QRY,param,CombCostVo.class);
    }

    @Override
    public ModCombInfoVo func_comb_info_mod(ModCombInfoForm param) {
        return ledT2RemoteService.callT2(FUNC_COMBINE_MOD_APPLY,param,ModCombInfoVo.class);
    }

    @Override
    public AvailableMoneyQryBackVo func_available_money_query(AvailableMoneyForm param) {
        return ledT2RemoteService.callT2(FUNC_BALANCEALL_QRY,param,AvailableMoneyQryBackVo.class);
    }


//    @Override
//    public AvailableMoneyVo func_available_money_query(AvailableMoneyForm param) {
//        return ledT2RemoteService.callT2(FUNC_BALANCEALL_QRY,param,AvailableMoneyVo.class);
//    }

    @Override
    public PositionQryBackVo func_position_query(PositionForm param) {
        return ledT2RemoteService.callT2(FUNC_STOCK_QRY,param,PositionQryBackVo.class);
    }

    @Override
    public WarehouseByDayQryBackVo func_warehouse_day_query(WarehouseByDayForm param) {
        return ledT2RemoteService.callT2(FUNC_COMENTRUST_DETAIL_QRY,param,WarehouseByDayQryBackVo.class);
    }

    @Override
    public WarehouseByHistoryQryBackVo func_warehouse_history_query(WarehouseByHistoryForm param) {
//        if("2033".equals(param.getCompany_id())) {
//            UserCache userCache = UserTokenUtil.queryUserToken(param.getUser_token());
//            if ("0".equals(userCache.getIsCapitalAccount())) {
//                WarehouseByHistoryQryBackVo warehouseByHistoryQryBackVo = new WarehouseByHistoryQryBackVo();
//                warehouseByHistoryQryBackVo.setData_list(Collections.EMPTY_LIST);
//                return warehouseByHistoryQryBackVo;
//            }
//        }
        return ledT2RemoteService.callT2(FUNC_COMENTRUST_DETAIL_HIS_QRY,param,WarehouseByHistoryQryBackVo.class);
    }

//    @Override
//    public BindCapitalAccountQryBackVo bindCapitalAccount(BindCapitalAccountForm param) {
//        return ledT2RemoteService.callT2(FUNC_BUSINACCT_BIND,param,BindCapitalAccountQryBackVo.class);
//    }


    @Override
    public BindCapitalAccountVo func_capital_account_bind(BindCapitalAccountForm param) {
        return ledT2RemoteService.callT2(FUNC_BUSINACCT_BIND,param,BindCapitalAccountVo.class);
    }

    @Override
    public UnbundCapitalAccountQryBackVo func_capital_account_unbund(@ServiceParam("param") UnbundCapitalAccountForm param) {
        return ledT2RemoteService.callT2(FUNC_BUSINACCT_UNBIND,param,UnbundCapitalAccountQryBackVo.class);
    }

//    @Override
//    public TradLoginQryBackVo tradLogin(@ServiceParam("param") TradLoginForm param) {
//        return ledT2RemoteService.callT2(FUNC_ACCT_LOGIN,param,TradLoginQryBackVo.class);
//    }


    @Override
    public TradLoginVo func_trad_login(TradLoginForm param) {
        String password = param.getPassword();
       //私钥解密过程
//        byte[] res= RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(), Base64.decode(password));
//        param.setPassword(password);
        TradLoginVo tradLogin= ledT2RemoteService.callT2(FUNC_ACCT_LOGIN,param,TradLoginVo.class);
        if("0".equals(tradLogin.getError_no())) {
            BrokerVO brokerVO = new BrokerVO();
            UserTokenUtil.setBGBrokerVO(brokerVO, param);
            UserCache userCache = new UserCache();
            userCache.setBrokerVO(brokerVO);
            userCache.setAccount_content(param.getAccount_content());
            userCache.setPassword_type(param.getPassword_type());
            userCache.setPassword(password);
            String user_token = UserTokenUtil.addUserToken(userCache);
            tradLogin.setUser_token(user_token);
        }
        return tradLogin;
    }

    @Override
    public RecommendInterestQryBackVo func_recommend_interest_query(RecommendInterestForm param) {
        return ledT2RemoteService.callT2(FUNC_COMUSER_RECOMMEND_QRY,param,RecommendInterestQryBackVo.class);
    }

    @Override
    public InterestRankQryBackVo func_interest_rank_query(InterestRankForm parma) {
        return ledT2RemoteService.callT2(FUNC_COMADVISER_ORDER_QRY,parma,InterestRankQryBackVo.class);
    }

//    @Override
//    public FocusInterestByQryBackVo addFocusInterest(FocusInterestByForm param) {
//        return ledT2RemoteService.callT2(FUNC_COMUSER_COLLECT,param,FocusInterestByQryBackVo.class);
//    }


    @Override
    public InterestFocusVo func_focus_interest_add(FocusInterestByForm param) {
        return ledT2RemoteService.callT2(FUNC_COMUSER_COLLECT,param,InterestFocusVo.class);
    }

//    @Override
//    public InterestDetailQryBackVo queryInterestDetail(InterestDetailForm param) {
//        return ledT2RemoteService.callT2(FUNC_COMADVISER_QRY,param,InterestDetailQryBackVo.class);
//    }


    @Override
    public InterestDetailVo func_interest_detail_query(InterestDetailForm param) {
        return ledT2RemoteService.callT2(FUNC_COMADVISER_QRY,param,InterestDetailVo.class);
    }

    @Override
    public InterestHistoryYieldQryBackVo func_interest_history_yield_query(InterestHistoryYieldForm param) {
        return ledT2RemoteService.callT2(FUNC_COMUSER_HIS_INCOMERATIO_QRY,param,InterestHistoryYieldQryBackVo.class);
    }

    @Override
    public MessageAndNoticeQryBackVo func_message_and_notice_get(MessageAndNoticeForm param) {
        return ledT2RemoteService.callT2(MESSAGE_AND_NOTICE_GET,param,MessageAndNoticeQryBackVo.class);
    }

//    @Override
//    public InterestCertQryBackVo addInterestCert(InterestCertForm param) {
//        return ledT2RemoteService.callT2(FUNC_INTEREST_CERT,param,InterestCertQryBackVo.class);
//    }

    @Override
    public InterestCertVo func_interest_cert__add(InterestCertForm param) {
        return ledT2RemoteService.callT2(FUNC_INTEREST_CERT,param,InterestCertVo.class);
    }

    @Override
    public CombCompositionVo func_comb_composition_query(CombCompositionForm param) {
        return ledT2RemoteService.callT2(COMBINE_BASIC_INFO_QRY,param,CombCompositionVo.class);
    }

    @Override
    public CombHisComentrustQryBackVo func_comb_his_comentrust(CombHisComentrustForm param) {
//        if("2033".equals(param.getCompany_id())) {
//            UserCache userCache = UserTokenUtil.queryUserToken(param.getUser_token());
//            if ("0".equals(userCache.getIsCapitalAccount())) {
//                CombHisComentrustQryBackVo combHisComentrustQryBackVo = new CombHisComentrustQryBackVo();
//                combHisComentrustQryBackVo.setData_list(Collections.EMPTY_LIST);
//                return combHisComentrustQryBackVo;
//            }
//        }
        return ledT2RemoteService.callT2(COMB_HIS_COMENTRUST,param,CombHisComentrustQryBackVo.class);
    }

    @Override
    public ComuserSignVo func_comuser_sign(ComuserSignForm param) {
        return ledT2RemoteService.callT2(COMUSER_SIGN,param,ComuserSignVo.class);
    }

    @Override
    public ComuserSignCancelVo func_comuser_sign_cancel(ComuserSignCancelForm param) {
        return ledT2RemoteService.callT2(COMUSER_SIGN_CANCEL,param,ComuserSignCancelVo.class);
    }

    @Override
    public ComadviserSignQryBackVo func_comadviser_sign_qry(ComadviserSignQryForm param) {
        return ledT2RemoteService.callT2(COMADVISER_SIGN_QRY,param,ComadviserSignQryBackVo.class);
    }

    @Override
    public FareargVo func_farearg_set(FareargFrom param) {
        return ledT2RemoteService.callT2(EXTCOMACCT_FAREARG_SET,param,FareargVo.class);
    }

    @Override
    public FareargQryVo func_farearg_qry(FareargQryForm param) {
//        String intest=param.getUser_type();//101投顾
        String adviserId=param.getAdviser_id();
//        if( (adviserId==null || adviserId.equals("")) && (("101").equals(userCache.getUser_type())) ){
        if(adviserId==null || adviserId.equals("")){
            param.setAdviser_id(param.getUser_id());
        }
        return ledT2RemoteService.callT2(EXTCOMACCT_FAREARG_QRY,param,FareargQryVo.class);
    }

    @Override
    public ComuserSignQryBackVo func_comuser_sign_qry(ComuserSignQryForm param) {
        return ledT2RemoteService.callT2(COMUSER_SIGN_QRY,param,ComuserSignQryBackVo.class);
    }

    @Override
    public BasicUserInfoQryVo func_basic_user_info(BasicUserInfoForm param) {
        return ledT2RemoteService.callT2(USER_BASIC_INFOMATION_QRY,param,BasicUserInfoQryVo.class);
    }

    @Override
    public ComuserCollectCancelVo func_comuser_collect_cancel(ComuserCollectCancelForm param) {
        return ledT2RemoteService.callT2(COMUSER_COLLECT_CANCEL,param,ComuserCollectCancelVo.class);
    }

    @Override
    public AuthuserRegbindVo func_authuser_regbind(AuthuserRegbindForm param) {
        return ledT2RemoteService.callT2(AUTHUSER_REGBIND,param,AuthuserRegbindVo.class);
    }

    @Override
    public SendMsgVo func_send_msg(SendMsgForm param) {
        return ledT2RemoteService.callT2(SEND_MSG,param,SendMsgVo.class);
    }

    @Override
    public SendMsgVo func_send_message(SendMsgForm param) {
//        ConfigNoForm configNoForm= JSON.parseObject(JSON.toJSONString(param), ConfigNoForm.class);
//        configNoForm.setConfig_no("46007");
//        param.setOwn_company_id(param.getSendercomp_id());
//        ConfigNoVo configNoVo = ledT2RemoteService.callT2(CONFIGURE_NO,configNoForm,ConfigNoVo.class);
//        param.setSms_authkey("236959ea4bd846eab0eb76d6c70ce666");
        param.setSystem_no(5L);
        return ledT2RemoteService.callT2(SEND_MESSAGE,param,SendMsgVo.class);
    }

    public AuthuserRegbindVo func_services_authuser_regbind(AuthuserRegbindForm param){
        param.setReg_source(param.getBusinsys_no());
        param.setSystem_no(1L);
        return ledT2RemoteService.callT2(SERVICES_AUTHUSER_REGBIND,param,AuthuserRegbindVo.class);
    }

    @Override
    public ComentrustDetailReasonModVo func_comentrust_detail_reason_mod(ComentrustDetailForm param) {
        return ledT2RemoteService.callT2(COMENTRUST_DETAIL_REASON_MOD,param,ComentrustDetailReasonModVo.class);
    }

    @Override
    public UserAdviserHisQryVo func_user_adviser_his_qry(UserAdviserHisQryForm param) {
        return ledT2RemoteService.callT2(USER_ADVISER_HIS_QRY,param,UserAdviserHisQryVo.class);
    }

    @Override
    public ProdcodeAdviserHisQryVo func_prodcode_adviser_his_qry(ProdcodeAdviserHisQryForm param) {
        return ledT2RemoteService.callT2(PRODCODE_ADVISER_HIS_QRY,param,ProdcodeAdviserHisQryVo.class);
    }

    public BatchStocksEntrustVo func_batch_stock_entrust( BatchStocksEntrustForm param){
        return ledT2RemoteService.callT2(FUNC_BATCH_STOCK_ENTRUST,param,BatchStocksEntrustVo.class);
    }
}
